/*大转盘抽奖页面*/
(function () {
  var ua = navigator.userAgent.toLocaleLowerCase()
  var that = window.commonFunc
  var vm = new Vue({
    el: '#receive-info',
    data: {
      cityList:[],
      prizeshortname:that.getQueryString('prizeshortname'), // 1 一等级 2 二等奖 3 三等奖
      userId:'', // 用户id
      prize_info:[
        {
          id:'1',
          name:'语文',
          sub:[
            {id:'1',name:'人教版'},
          ]
        },
        {
          id:'2',
          name:'英语',
          sub:[
            {id:'1',name:'人教版'},
            {id:'2',name:'外研版'},
            {id:'3',name:'译林牛津版'},
            {id:'4',name:'冀教版'}
          ]
        },
        {
          id:'3',
          name:'数学',
          sub:[
            {id:'1',name:'人教版'},
            {id:'2',name:'北师版'},
            {id:'3',name:'沪科版'},
            {id:'4',name:'华师版'},
            {id:'5',name:'苏科版'}
          ]
        }
      ], // 一遍过，教材帮
      prize_info2:[
        {
          id:'1',
          name:'语文',
          sub:[
            {id:'1',name:'全国版'},
            {id:'2',name:'安徽版'},
            {id:'3',name:'福建版'},
            {id:'4',name:'广东版'},
            {id:'5',name:'河北版'},
            {id:'6',name:'河南版'},
            {id:'7',name:'湖北版'},
            {id:'8',name:'江西版'},
            {id:'9',name:'山西版'},
            {id:'10',name:'陕西版'}
          ]
        },
        {
          id:'2',
          name:'英语',
          sub:[
            {id:'1',name:'全国版'},
            {id:'2',name:'安徽版'},
            {id:'3',name:'福建版'},
            {id:'4',name:'广东版'},
            {id:'5',name:'河北版'},
            {id:'6',name:'河南版'},
            {id:'7',name:'湖北版'},
            {id:'8',name:'江西版'},
            {id:'9',name:'山西版'},
            {id:'10',name:'陕西版'}
          ]
        },
        {
          id:'3',
          name:'数学',
          sub:[
            {id:'1',name:'全国版'},
            {id:'2',name:'安徽版'},
            {id:'3',name:'福建版'},
            {id:'4',name:'广东版'},
            {id:'5',name:'河北版'},
            {id:'6',name:'河南版'},
            {id:'7',name:'湖北版'},
            {id:'8',name:'江西版'},
            {id:'9',name:'山西版'},
            {id:'10',name:'陕西版'}
          ]
        }
      ], // 中考45套
      prize_info3:[
        {id:'1',name:'语文'},
        {id:'2',name:'英语'},
        {id:'3',name:'数学【文科】'},
        {id:'4',name:'数学【理科】'}
      ], // 调研6辑、特快专递5期
      userForm: {
        prizeid:that.getQueryString('prizeid'),
        username: '',
        userphone: '',
        pidname:'',
        provinceid:'',
        cityid:'',
        countyid:'',
        useraddress:'',
        lottery_data:'',
      },
      lottery_data_txt:'',
      prize_data:{
        mj:'', // 密卷
        grade:'', // 年级
        grade_name:'', // 初中或者高中
        jcb:'', // 初中
        ybg:'', // 初中
        scj:'', // 初中+高中
        zk:'', // 九年级 中考45套
        dy:'', // 高中
        tk:'', // 高中
      },
      errorMsg: '出错了',
      errorDialog: false,
      phone: null,
      isSave:true, // 是否显示保存按钮
      isFill:false, // 没有填写过信息
    },
    created: function () {
      // var vConsole = new VConsole();
    },
    computed: {
      canSaveUser : function () {
        if(parseInt(this.prizeshortname) == 3){
          if(this.userForm.username && this.userForm.userphone && this.userForm.pidname && this.userForm.useraddress){
            return true
          }else{
            return false
          }
        }
        if(this.userForm.username && this.userForm.userphone && this.userForm.pidname && this.userForm.useraddress && (this.prize_data.mj || ((this.prize_data.jcb && this.prize_data.ybg) || this.prize_data.zk || (this.prize_data.dy && this.prize_data.tk) ))){
          return true
        }else{
          return false
        }

      },
    },
    mounted: function () {
      that.pixel2rem();
      this.getCity();
      this.getPrizeInfo();
    },
    methods: {
      saveBtn:function () {
        var _this = this;
        var prize_mj = '2019临考押题密卷一套-'+_this.prize_data.mj;
        var prize_jcb = _this.prize_data.grade+'教材帮'+_this.prize_data.jcb;
        var prize_ybg = _this.prize_data.grade+'一遍过'+_this.prize_data.ybg;
        var prize_scj = '拜托了，素材君(任意三本)';
        var prize_zk = _this.prize_data.grade+'中考45套'+_this.prize_data.zk;
        var prize_dy = _this.prize_data.grade+'调研6辑'+_this.prize_data.dy;
        var prize_tk = _this.prize_data.grade+'特快专递5期'+_this.prize_data.tk;
        if(parseInt(this.prizeshortname) == 1){
          // 一等奖
          _this.userForm.lottery_data = [prize_mj].join(';')
        }else if(parseInt(this.prizeshortname) == 2){
          if(_this.prize_data.grade_name == '初中'){
            if(_this.prize_data.grade != '初中九年级'){
              _this.userForm.lottery_data = [prize_jcb,prize_ybg,prize_scj].join(';')
            }else{
              _this.userForm.lottery_data = [prize_zk,prize_scj].join(';')
            }
          }else{
            // 高中
            _this.userForm.lottery_data = [prize_dy,prize_tk,prize_scj].join(';')
          }
        }else if(parseInt(this.prizeshortname) == 3){
          _this.userForm.lottery_data = [prize_scj].join(';')
        }
        if(_this.canSaveUser){
          $.ajax({
            url:api.saveInfo+'&id='+that.getQueryString("id"),
            data:JSON.stringify(_this.userForm),
            type: 'PUT',
            headers: {
              "Content-Type": "application/json",
              "X-HTTP-Method-Override": "PUT"
            },
            success:function(data){
              // $('#loading').fadeOut(300)
              if(data){
                if(data.error){
                  _this.errorDialog = true
                  _this.errorMsg = data.msg
                }else{
                  // 填写成功后，刷新下页面 或者重新调一下获取基本信息接口
                  that.toast({
                    msg: '保存成功'
                  });
                  _this.getPrizeInfo();

                }
              }
            },
            error:function(data){
              // $('#loading').fadeOut(300)
            }
          })
        }
      },
      getPrizeInfo:function () {
        var _this = this;
        $.ajax({
          url:api.getPrizeInfo+'&prizeid='+that.getQueryString("prizeid"),
          data:'',
          type: 'GET',
          success:function(data){
            // $('#loading').fadeOut(300)
            if(data){
              if(data.error){
                _this.errorDialog = true
                _this.errorMsg = data.msg
              }else{
                if(data.isupdate != '1'){
                  // 不可以填写
                  _this.isFill = true;
                }
                if(data.userinfo.pidname != ''){
                  // 填写过信息
                  _this.isFill = true;
                  _this.userForm.username = data.userinfo.username;
                  _this.userForm.userphone = data.userinfo.userphone;
                  _this.userForm.pidname = data.userinfo.pidname;
                  _this.userForm.useraddress = data.userinfo.useraddress;
                  _this.lottery_data_txt = data.userinfo.lottery_data.split(';');

                }else{
                  _this.isFill = false;

                }
              }
            }
          },
          error:function(data){
            $('#loading').fadeOut(300)
          }
        })
      },
      getCity:function () {
        var _this = this;
        $.ajax({
          url:api.getCity+'&pid=',
          data:'',
          type: 'GET',
          success:function(data){
            $('#loading').fadeOut(300)
            if(data){
              if(data.error){
                _this.errorMsg = data.msg
                _this.errorDialog = true
              }else{
                _this.cityList = data
                if(_this.cityList){
                  _this.initCity();
                }
              }
            }
          },
          error:function(data){
            $('#loading').fadeOut(300)
          }
        })
      }, // 获取省市区
      initCity: function (arr) {
        var _this = this
        window.CitySelect = new MobileSelect({
          trigger: '#City',
          title: '',
          wheels: [
            {data: _this.cityList}
          ],
          position: [2,0],
          callback: function (indexArr, data) {
            console.log(indexArr)
            console.log(data)
            _this.userForm.pidname = ''
            data.forEach(function (v) {
              console.log(v)
              _this.userForm.pidname += v.name
            })
            for(var i=0; i<data.length;i++){
              _this.userForm.provinceid=data[0].id;
              _this.userForm.cityid=data[1].id;
              _this.userForm.countyid=data[2].id;
            }
            console.log(_this.userForm)
          }
        })
      },
      City: function () {
        window.CitySelect.show()
      },
      initGradeData:function () {
        var _this = this;
        if(!window.hasOwnProperty('grade')){
          window.grade = new MobileSelect({
            trigger: '#Grade',
            title: '年级选择',
            wheels: [
              {data:[
                {
                  id:'1',
                  name:'初中',
                  sub:[
                    {id:'1',name:'七年级'},
                    {id:'2',name:'八年级'},
                    {id:'3',name:'九年级'}
                  ]
                },
                {id:'2',name:'高中'},
              ]}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.grade = '';
              data.forEach(function (v) {
                _this.prize_data.grade += v.name;
              })
              _this.prize_data.grade_name = data[0].name;
              // 选择年级的时候，清空其它选项
              _this.prize_data.jcb = ''; // 初中
              _this.prize_data.ybg = ''; // 初中
              // _this.prize_data.scj = ''; // 初中+高中
              _this.prize_data.zk = ''; // 九年级 中考45套
              _this.prize_data.dy = ''; // 高中
              _this.prize_data.tk = ''; // 高中
              _this.prize_data.mj = '';
            }
          });
        }
      },
      Grade: function () {
        this.initGradeData();
        window.grade.show()
      },
      initJCBData:function () {
        var _this = this;
        if(!window.hasOwnProperty('JCBData')){
          window.JCBData = new MobileSelect({
            trigger: '#JCBData',
            title: '科目版本',
            wheels: [
              {data:_this.prize_info}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.jcb = '';
              data.forEach(function (v) {
                _this.prize_data.jcb += v.name;
              })
              // _this.prize_data.jcb = data[0].name;
            }
          });
        }

      },
      JCBData: function () {
        this.initJCBData()
        window.JCBData.show()
      },
      initYBGData:function () {
        var _this = this;
        if(!window.hasOwnProperty('YBGData')){
          window.YBGData = new MobileSelect({
            trigger: '#YBGData',
            title: '科目版本',
            wheels: [
              {data:_this.prize_info}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.ybg = '';
              data.forEach(function (v) {
                _this.prize_data.ybg += v.name;
              })
              // _this.prize_data.ybg = data[0].name;
            }
          });
        }

      },
      YBGData: function () {
        this.initYBGData()
        window.YBGData.show()
      },
      initZKData:function () {
        var _this = this;
        if(!window.hasOwnProperty('ZKData')){
          window.ZKData = new MobileSelect({
            trigger: '#ZKData',
            title: '科目版本',
            wheels: [
              {data:_this.prize_info2}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.zk = '';
              data.forEach(function (v) {
                _this.prize_data.zk += v.name;
              })
              // _this.prize_data.ybg = data[0].name;
            }
          });
        }

      },
      ZKData: function () {
        this.initZKData()
        window.ZKData.show()
      },
      initDYData:function () {
        var _this = this;
        if(!window.hasOwnProperty('DYData')){
          window.DYData = new MobileSelect({
            trigger: '#DYData',
            title: '科目版本',
            wheels: [
              {data:_this.prize_info3}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.dy = '';
              data.forEach(function (v) {
                _this.prize_data.dy += v.name;
              })
              // _this.prize_data.ybg = data[0].name;
            }
          });
        }
      },
      DYData: function () {
        this.initDYData()
        window.DYData.show()
      },
      initTKData:function () {
        var _this = this;
        if(!window.hasOwnProperty('TKData')){
          window.TKData = new MobileSelect({
            trigger: '#TKData',
            title: '科目版本',
            wheels: [
              {data:_this.prize_info3}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.tk = '';
              data.forEach(function (v) {
                _this.prize_data.tk += v.name;
              })
              // _this.prize_data.ybg = data[0].name;
            }
          });
        }

      },
      TKData: function () {
        this.initTKData()
        window.TKData.show()
      },
      initMJData:function () {
        var _this = this;
        if(!window.hasOwnProperty('MJData')){
          window.MJData = new MobileSelect({
            trigger: '#MJData',
            title: '文/理科',
            wheels: [
              {data:[
                {id:'1',name:'文科'},
                {id:'2',name:'理科'},
              ]}
            ],
            callback:function(indexArr, data){
              console.log(data);
              _this.prize_data.mj = '';
              data.forEach(function (v) {
                _this.prize_data.mj += v.name;
              })

            }
          });
        }

      },
      MJData: function () {
        this.initMJData()
        window.MJData.show()
      },
      Validation: function (arg) {
        if (!arg.result) {
          that.toast({
            msg: arg.msg
          });
          return false;
        } else {
          return true;
        }
      }, //验证
      ValidateName: function () {
        var arg = this.userForm.username;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '姓名不能为空'
        };
      },
      ValidatePhone:function () {
        var arg = this.userForm.userphone;
        if (arg.length > 0) {
          return arg.length == 11 ? {
            result: true,
            msg: ''
          } : {
            result: false,
            msg: '请输入合法的手机号'
          };
        } else {
          return {
            result: false,
            msg: '手机号不能为空'
          }
        }
      },
      ValidatePidname:function () {
        var arg = this.userForm.pidname;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '所在地区不能为空'
        };
      },
      ValidateAddress:function () {
        var arg = this.userForm.useraddress;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '具体地址不能为空'
        };
      },
      ValidateMJ:function () {
        var arg = this.prize_data.mj;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择文/理科'
        };
      },
      ValidateJCB:function () {
        var arg = this.prize_data.jcb;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择教材帮科目版本'
        };
      },
      ValidateYBG:function () {
        var arg = this.prize_data.ybg;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择一遍过科目版本'
        };
      },
      ValidateZK:function () {
        var arg = this.prize_data.zk;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择中考45套科目版本'
        };
      },
      ValidateDY:function () {
        var arg = this.prize_data.dy;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择调研6辑科目版本'
        };
      },
      ValidateTK:function () {
        var arg = this.prize_data.tk;
        return arg.length > 0 ? {
          result: true,
          msg: ''
        } : {
          result: false,
          msg: '请选择特快专递5期科目版本'
        };
      },
      closeErrorDialog: function() {
        window.location.reload()
      }
    }
  })
})()
